//= require <controls/HtmlControl>
//= require <templating/jstparser>

namespace("controls::HtmlElement", {
	"@inherits" : controls.HtmlControl,
    '@css' : {
        "img" : {}
    },
    
	initializeElement : function(params) {
        this.load_css();
        params = params||{};
        params.element = (params.element||this.element);
		var createElement = function createElement(html_string) {
	        var element = html_string.toHtmlElement();
	        if (element && Object.isElement(element)) {
	            return element;}
	        else {
	            throw Error("Error in initializeElement() - Could not parse html:\n" + html_string);}
	    };
        
		if (params && (params.element && Object.isElement(params.element))) {
            this.element = params.element;
			this.initializeChildren();
        }
        else if(params && typeof params.element === "string") {
            if (/^\<(.)+\>.*/.test(params.element)) {
                var evaledHtml = Jst.evaluate_jst(params.element,this.model);
    			var el = createElement(evaledHtml);
                this.element = el;
    			this.initializeChildren();
            }
            else if (/^(.*)\.([A-Za-z]{1,5})$/.test(params.element)) {
                this.element = document.createElement("div");
                var self = this;
                var path = this.resourcepath(params.element);
         	    var htmlRequest = new MooTools.Request({
              	    method: 'GET', 
               	    url: path,
                	async:true,
                	isSuccess : function(result_html){
                        var res = htmlRequest.xhr.responseText;
             	        var evaledHtml = Jst.evaluate_jst(res,self.model);
                        var el = createElement(evaledHtml);
                            $(el).replaces(self.element);
                            self.element = el;
                            self.initializeChildren();
        			}.bind(self)
                });
                try{
                    htmlRequest.send();
                } catch(e){
                    alert("Error connecting to resource:\n" + path + "\n\n" + e.message||"");
                }
      	    }
        }
        else if(params.element && typeof params.element === "function") {
            this.element = Jst.evaluate_jst(res,self.model);
    		this.initializeChildren();
        }
        else {
            this.element = document.createElement("div");
            this.element.innerHTML = "Loading...";
            this.applyCssExpressions();
		    this.fireEvent("element-initialized");
        }
	},
    
    resourcepath : function(filename){
        if (/^(.*)\.([A-Za-z]{1,5})$/.test(filename)) {
            var parts = {
                "cnd-domain" : cdn(),
                "javascripts-root" : config.loadpaths.LIBROOT,
                
                "filetype" : RegExp.$2,
                "filename" : RegExp.$1,
                "ns-to-path" : this.namespace.replace(/\:{2}|\./ig,"/")
            };
            var path = "cnd-domain/javascripts-root" + "ns-to-path/filetype/filename.filetype";
                path = path.replace("cnd-domain", parts["cnd-domain"]).
                    replace("javascripts-root",parts["javascripts-root"]).
                    replace("ns-to-path", parts["ns-to-path"]).
                    replace("filetype",parts["filetype"]).
                    replace("filetype",parts["filetype"]).
                    replace("filename",parts["filename"]);
                return path || "";
        }
    },
    
    
    fixPNGimg : function(pngs) {
        if((navigator.DeviceInfo.browser != "MSIE")) 
            return;
        var fixit =  function(png) {
           var src = png.src;
           if(!(src.indexOf("png") >= 0)) return;
           // set width and height
           //if (!png.style.width) { png.style.width = $(png).width(); }
           //if (!png.style.height) { png.style.height = $(png).height(); }
           // replace by blank image
           png.onload = function() { };
           png.src = "http://www.triosolisti.com/images/transparent_spacer.gif";
           png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
         }
         
        pngs = pngs || this.getElements("img");
        if(pngs && pngs instanceof Array) {
            for(i=0; i<= pngs.length-1; i++) {
                var png = pngs[i];
                fixit(png);
            }
        }         
	}
});
